{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Javaプログラミングの基礎を学ぶ\n",
    "\n",
    "* ふたつのクラス\n",
    "    * Person\n",
    "        * フィールド（変数）やコンクラスタを定義\n",
    "        * コンストラクタ\n",
    "            * <font color=\"red\">クラス名と同じ名前のメソッド</font> (Personメソッド)\n",
    "            * インスタンス化されたときに自動で呼び出される\n",
    "        * 仮引数\n",
    "            * _name, _age, _address\n",
    "                * フィールド名の変数と区別するために先頭にアンダースコアをつけている\n",
    "                * フィールド名と仮引数の名前を同じにする場合は<font color=\"red\">フィールド名にはthisをつける</font>\n",
    "                    * `this.name = name;`\n",
    "            * データ型と変数名を書く\n",
    "            * データ型は呼び出し元の実引数の値に合わせる\n",
    "    * PersonTest\n",
    "        * クラスのインスタンス化や画面表示を行う\n",
    "        * mainメソッドがある\n",
    "            * mainメソッドは、<font color=\"red\">Javaアプリケーションを実行したときに最初に呼び出される</font>\n",
    "                * クラスのインスタンス化や画面表示はmainメソッドの中に書く\n",
    "        * クラスのインスタンス化\n",
    "            * new演算子を使う\n",
    "            * <font color=\"red\">クラス名</font> 変数名 = new <font color=\"red\">コンストラクタ</font>(実引数)\n",
    "            * 実引数は「\"」で囲み、複数ある場合は「,」で区切る\n",
    "        * インスタンスのフィールド値の参照\n",
    "            * インスタンス名.フィールド名\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Taro Age:21 Address:Tokyo\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class Person {\n",
    "  //# フィールド（変数）の定義\n",
    "  String name;\n",
    "  int age;\n",
    "  String address;\n",
    "  \n",
    "  //# コンクラスタの定義。仮引数とフィールド名を区別するために仮引数にはアンダースコアをつけている\n",
    "  Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "}\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "\n",
    "    //# new演算してインスタンスを生成。頭のPersonはクラス名。newの後ろはコンストラクタ\n",
    "    Person taro = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    System.out.println(\"Name:\" + taro.name + \" Age:\" + taro.age + \" Address:\" + taro.address);\n",
    "\n",
    "    Person hanako = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "    System.out.println(\"Name:\" + taro.name + \" Age:\" + taro.age + \" Address:\" + taro.address);\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "### コメントアウトすら文字化けするのでエンコーディングを指定\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## 同じようなコードはメソッドでまとめる\n",
    "\n",
    "* メソッドの定義\n",
    "\n",
    "```\n",
    "戻り値の型 メソッド名(仮引数のリスト){\n",
    "  内容\n",
    "  return 戻り値;\n",
    "}\n",
    "```\n",
    "\n",
    "* 戻り値の型\n",
    "    * void\n",
    "        * 戻り値が無い場合\n",
    "        * <font color=\"red\">returnを省略できる</font>\n",
    "* 仮引数のリスト\n",
    "    * 仮引数が無い場合は省略できる\n",
    "* <font color=\"red\">メソッドの呼び出しには必ずカッコ()がつく</font>\n",
    "    * すぐにフィールドと区別がつく\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class Person {\n",
    "  String name;\n",
    "  int age;\n",
    "  String address;\n",
    "  \n",
    "  Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  //# メソッドを定義\n",
    "  void say(){\n",
    "    System.out.println(\"Name:\" + name + \" Age:\" + age + \" Address:\" + address);\n",
    "  }\n",
    "}\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "\n",
    "    Person taro = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    //# メソッドの呼び出しにはカッコがつく\n",
    "    taro.say();\n",
    "\n",
    "    Person hanako = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "    hanako.say();\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Javaメソッドに戻り値を返させる\n",
    "\n",
    "* 戻り値\n",
    "    * メソッドの呼び出し元にメソッドの行った処理の結果として返す値\n",
    "    * returnを使うので別名リターン値とも呼ばれる\n",
    "    * 戻り値の種類\n",
    "        * 数値\n",
    "        * 文字列\n",
    "        * true/false"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name:Taro Age:21 Address:Tokyo\n",
      "Name:Hanako Age:18 Address:Hokkaido\n"
     ]
    }
   ],
   "source": [
    "%%bash\n",
    "### 変数の設定\n",
    "name=PersonTest\n",
    "\n",
    "### ソースの編集\n",
    "cat <<- EOS > ${name}.java\n",
    "\n",
    "class Person {\n",
    "  String name;\n",
    "  int age;\n",
    "  String address;\n",
    "  \n",
    "  Person(String _name, int _age, String _address){\n",
    "    name = _name;\n",
    "    age = _age;\n",
    "    address = _address;\n",
    "  }\n",
    "  \n",
    "  //# メソッドを定義。返り値は文字列型\n",
    "  String say(){\n",
    "    String str = \"Name:\" + name + \" Age:\" + age + \" Address:\" + address;\n",
    "    return str;\n",
    "  }\n",
    "}\n",
    "\n",
    "public class ${name} {\n",
    "  public static void main (String[] args) {\n",
    "\n",
    "    Person taro = new Person(\"Taro\", 21, \"Tokyo\");\n",
    "    //# 戻り値の結果を出力\n",
    "    System.out.println(taro.say());\n",
    "\n",
    "    Person hanako = new Person(\"Hanako\", 18, \"Hokkaido\");\n",
    "    System.out.println(hanako.say());\n",
    "\n",
    "  }\n",
    "}\n",
    "EOS\n",
    "\n",
    "### コンパイル\n",
    "#javac ${name}.java\n",
    "javac -encoding UTF-8 ${name}.java\n",
    "\n",
    "### 実行\n",
    "java ${name}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
